package net.adst.util;

import net.adst.beans.NaemTec;
import net.adst.resources.Constants;
import net.adst.resources.VariableNames;
import net.adst.ui.mainframe.tabs.AdstTabPanel;
import net.adst.util.fileFilters.FileOutputStreamFactory;
import org.apache.poi.hssf.usermodel.*;
import org.apache.poi.hssf.util.HSSFColor;
import org.apache.poi.ss.util.CellRangeAddress;

import javax.swing.table.DefaultTableModel;
import java.io.*;
import java.util.ArrayList;

/**
 * Created with IntelliJ IDEA.
 * User: alekseipot
 * Date: 19.11.12
 * Time: 4:32
 * To change this template use File | Settings | File Templates.
 */
public class FileHelper {
    public static void createResultExcelFile(File file) {
        int index = AdstTabPanel.getIndex();
        switch (index) {
            case 0: {
                try {
                    if (file.exists()) {
                        file.delete();
                    }
                    FileOutputStream out = FileOutputStreamFactory.getFileOutputStreamFromFactory(file);
                    if (out == null) {
                        return;
                    }

                    DefaultTableModel model = (DefaultTableModel) DataManager.getObject(VariableNames.UI_NAEM_DETAILED_MODEL);
                    HSSFWorkbook wb = new HSSFWorkbook();
                    HSSFSheet sheet = wb.createSheet("Наем. тех.");
                    HSSFRow row = null;
                    row = sheet.createRow(0);
                    HSSFCellStyle style = wb.createCellStyle();
                    style.setFillBackgroundColor(HSSFColor.GREEN.index);
                    style.setFillForegroundColor(HSSFColor.RED.index);
                    CellRangeAddress cellRangeAddress = new CellRangeAddress(0, 0, 0, 15);
                    sheet.setAutoFilter(cellRangeAddress);

                    for (int i = 0; i < Constants.NAEM_НEADER.length; i++) {
                        HSSFCell cell = row.createCell(i);
                        cell.setCellValue(Constants.NAEM_НEADER[i]);
                        cell.setCellStyle(style);
                    }


                    for (int j = 0; j < model.getRowCount(); j++) {
                        row = sheet.createRow(1 + j);
                        for (int i = 0; i < Constants.NAEM_НEADER.length; i++) {
                            if (i != Constants.NAEM_НEADER.length - 1) {
                                HSSFCell cell = row.createCell(i);
                                cell.setCellValue((String) model.getValueAt(j, i));
                            } else {
                                ArrayList<NaemTec> rows = (ArrayList<NaemTec>) DataManager.getObject(VariableNames.TEC_NAEM_USING_ROWS);
                                String[] comment = rows.get(j).getComment().split("\n");
                                for (int k = 0; k < comment.length; k++) {
                                    HSSFCell cell = row.createCell(i + k);
                                    cell.setCellValue(comment[k]);
                                }
                            }
                        }
                    }
                    wb.write(out);
                    out.close();
                    break;
                } catch (FileNotFoundException e) {
                    e.printStackTrace();  //To change body of catch statement use File | Settings | File Templates.
                } catch (IOException e) {
                    e.printStackTrace();  //To change body of catch statement use File | Settings | File Templates.
                }

            }
            case 1: {
                HSSFWorkbook wb = new HSSFWorkbook();
                HSSFSheet sheet = wb.createSheet("new sheet");
                HSSFRow row = null;
            }
        }
    }


    public static boolean verifyFileName(File directory, File file) {
        String fileName = file.getName();
        if (!file.getName().contains(".xls")) {
            fileName += ".xls";
        }
        File[] files = null;
        if (directory.isDirectory()) {
            files = directory.listFiles(new FileFilter() {
                @Override
                public boolean accept(File pathname) {
                    int position = pathname.getName().lastIndexOf(".");
                    String filename = pathname.getName().substring(position + 1);
                    return filename.contains("xls");
                }
            });
        }
        for (File f : files) {
            if (f.getName().equals(fileName)) {
                return false;
            }
        }
        return true;
    }


}
